package com.ecas.code.generator.controller;

import com.alibaba.fastjson.JSON;
import com.ecas.code.generator.annotation.PbParam;
import com.ecas.code.generator.dto.ParamterDTO;
import com.ecas.code.generator.dto.ResultBean;
import com.ecas.code.generator.dto.PageResultBean;
import com.ecas.code.generator.service.CodeGeneratorService;
import com.google.common.collect.Sets;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Controller
@RequestMapping(value = "/codegenerator")
public class CodeGeneratorController {

    @Autowired
    private CodeGeneratorService codeGeneratorService;

    @RequestMapping(value = "listTableInfo")
    @ResponseBody
    public ResultBean listTableInfo(@RequestParam  Map<String,Object> reqParams) {
        Integer totalCount = codeGeneratorService.countTable(reqParams);

        PageResultBean pageResultBean = new PageResultBean();
        pageResultBean.setTotalCount(totalCount);
        pageResultBean.setPageParams(reqParams);

        List<Map<String, Object>> mapList = codeGeneratorService.queryTable(reqParams);
        pageResultBean.setList(mapList);
        ResultBean result = new ResultBean();
        result.setPageResultBean(pageResultBean);
      return result;
    }

    @RequestMapping(value = "codeGenerate")
    @ResponseBody
    public void code(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        String tables = httpServletRequest.getParameter("tables");
        String[] tableNames = new String[]{};
        tableNames = JSON.parseArray(tables).toArray(tableNames);
        Set<String> tableNameSet = Sets.newHashSet(Arrays.asList(tableNames));
        String fileName = "code-generator.zip";
        byte[]  codeBytes = codeGeneratorService.generatorCode(tableNameSet);

        String encodeFileName = URLEncoder.encode(fileName,"utf-8");
        httpServletResponse.reset();
        httpServletResponse.setHeader("Content-Disposition","attachment;fileName="+encodeFileName);
        httpServletRequest.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/zip;charset=UTF-8");
        IOUtils.write(codeBytes,httpServletResponse.getOutputStream());
    }


    @RequestMapping(value = "protoGenerate")
    @ResponseBody
    public void codeGenerate(@RequestParam  List<ParamterDTO> paramterDTOList, HttpServletResponse httpServletResponse) throws IOException {
        byte [] codeContext=    codeGeneratorService.generatCode(paramterDTOList);
        httpServletResponse.reset();
        String fileName = "code-generator.zip";
        String encodeFileName = URLEncoder.encode(fileName,"utf-8");
        httpServletResponse.setHeader("Content-Disposition","attachment;fileName="+encodeFileName);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/zip;charset=UTF-8");
        IOUtils.write(codeContext,httpServletResponse.getOutputStream());
    }


}
